<?php
namespace app\api\controller;
use app\model\Token as TokenModel;
use think\Db;


class Index extends Base
{   
    protected $noCheckToken = ['products','goods','homeModule'];
    
    /**
     * @Author   CCH
     * @DateTime 2020-05-23T12:10:10+0800
     * @return   数据列表
     */
    public function products(){
        $categorys = Db::name('wx_product_category')->where('is_home',1)->select();
        $list = [];
        foreach ($categorys as $category) {
            $product = Db::name('wx_product')->where('product_category_id',$category['id'])->order('sort desc,id desc')->limit(3)->select();
            if (!empty($product)) {
                foreach ($product as $k => $vo) {
                    if (!empty($vo['tags'])) {
                        $product[$k]['tags'] = explode(',',$vo['tags']);
                    }else{
                        $product[$k]['tags'] = [];
                    }

                    if (!empty($vo['images'])) {
                        $product[$k]['images'] = explode(',',$vo['images']);
                    }else{
                        $product[$k]['images'] = [];
                    }
                }
                $list[] = [
                    'category'=>$category,
                    'product'=>$product
                ];
            }
        }

        return apiResult('2000','获取成功',$list);
    }

    
    /**
     * @Author   CCH
     * @DateTime 2020-05-23T12:10:10+0800
     * @return   数据列表
     */
    public function goods(){
        $categorys = Db::name('wx_goods_category')->where([
            ['location','like','%sy%']
        ])->order('sort desc,id desc')->select();
        $list = [];
        foreach ($categorys as $category) {
            $data = Db::name('wx_goods')->where([
                ['goods_category_id','=',$category['id']],
                ['status','=',1]
            ])->order('sort desc,id desc')->limit(3)->select();
            if (!empty($data)) {
                foreach ($data as $k => $vo) {
                    if (!empty($vo['tags'])) {
                        $data[$k]['tags'] = explode(',',$vo['tags']);
                    }else{
                        $data[$k]['tags'] = [];
                    }
                }
                $list[] = [
                    'category'=>$category,
                    'goods'=>$data
                ];
            }
        }

        return apiResult('2000','获取成功',$list);
    }

    public function homeModule(){
        $list = Db::name('home_module')->order('module asc')->limit(6)->select();
        return apiResult('2000','获取成功',$list);
    }


}
